#include "DXTimer.h"


DXTimer::DXTimer(VOID)
{
	INT64 countsPerSec;
	QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
	m_secondPerCount = 1.0 / (DOUBLE)countsPerSec;
	m_stopTimer = FALSE;
}


DXTimer::~DXTimer(VOID)
{

}


DOUBLE DXTimer::DetalTime() const
{
	return m_detalTime;
}


DOUBLE DXTimer::TotalTime() const
{
	INT64 endTime;
	endTime = m_stopTimer ? m_stopTime : m_currentTime;
	return (endTime - m_baseTime - m_pausedTime) * m_secondPerCount;
}


VOID DXTimer::Reset()
{
	INT64 currentTime;
	QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);

	m_baseTime     = currentTime;
	m_previousTime = currentTime;
	m_pausedTime   = 0;
	m_detalTime    = 0;
	m_stopTimer    = FALSE;
}


VOID DXTimer::Stop()
{
	if (m_stopTimer) { return; }

	INT64 currentTime;
	QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);

	m_stopTime  = currentTime;
	m_stopTimer = TRUE;
}


VOID DXTimer::Start()
{
	if (!m_stopTimer) { return; }

	INT64 currentTime;
	QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);

	m_pausedTime   += currentTime - m_stopTime;
	m_previousTime  = currentTime;
	m_detalTime     = 0;
	m_stopTime      = 0;
	m_stopTimer     = FALSE;
}


VOID DXTimer::Tick()
{
	if (m_stopTimer) { m_detalTime = 0; return; }

	INT64 currentTime;
	QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);

	m_currentTime = currentTime;
	m_detalTime   = (m_currentTime - m_previousTime) * m_secondPerCount;

	m_previousTime = m_currentTime;
	if (m_detalTime < 0) { m_detalTime = 0; }
}